package com.codeyourlife.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.codeyourlife.service.SearchService;

import net.sf.json.JSONArray;

@Controller 
public class SearchController {
	 @Autowired
	 private SearchService searchService;
	 
	 @RequestMapping(value="/getSearchResult", produces = "text/html;charset=UTF-8")  
	 @ResponseBody
	 public String getSearchResult(HttpServletRequest request){
		 String searchContent = request.getParameter("searchContent");
		 int page = Integer.parseInt(request.getParameter("page")); 
		 System.out.println(searchContent + "  "+ page);
		/* List<Course> SearchCoure = searchService.getSearchCourse(searchContent); */
		 try {
				List<Object> courses = searchService.getSearchCourse(searchContent,page);
				JSONArray json = JSONArray.fromObject(courses);
				String str = json.toString();
				System.out.println(str);
				return str;
			} catch (Exception e) {
				System.out.println("CourseController错误: ");
			}
		 return null;
	 }
}
